package top.wu2020.lazy.ws.chat.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Slf4j
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    /**
     * 注册端点，发布或者订阅消息的时候，需要连接此端点
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry
                //添加端点
                .addEndpoint("/socket.io")
                //设置允许访问的源（所有）
                .setAllowedOriginPatterns("*")
                //开始sockjs支持
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //设置消息代理(服务端推送给客户端的路径前缀)
        registry.enableSimpleBroker( "/chat");
        //设置消息发布订阅的头(客户端发送数据给服务器的一个前缀)
        registry.setApplicationDestinationPrefixes("/");
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {

        registration.interceptors(new ChannelInterceptor() {
            @Override
            public Message<?> preSend(Message<?> message, MessageChannel channel) {
                log.info("preSend:{}",message.getPayload().toString());
                return ChannelInterceptor.super.preSend(message, channel);
            }

            @Override
            public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
                log.info("postSend:{}",message.getPayload().toString());
                ChannelInterceptor.super.postSend(message, channel, sent);
            }

            @Override
            public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {
                log.info("afterSendCompletion:{}",message.getPayload().toString());
                ChannelInterceptor.super.afterSendCompletion(message, channel, sent, ex);
            }

            @Override
            public boolean preReceive(MessageChannel channel) {
                return ChannelInterceptor.super.preReceive(channel);
            }

            @Override
            public Message<?> postReceive(Message<?> message, MessageChannel channel) {
                log.info("postReceive:{}",message.getPayload().toString());
                return ChannelInterceptor.super.postReceive(message, channel);
            }

            @Override
            public void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex) {
                log.info("afterReceiveCompletion:{}",message.getPayload().toString());
                ChannelInterceptor.super.afterReceiveCompletion(message, channel, ex);
            }
        });
    }
}